package com.xcc.dataStructures.demo03_link;

/**
 * 双向链表
 *
 * @author xiaocheng
 * @date 2020/11/20 10:55
 */
public class DoubleLinkedDemo {

    public static void main(String[] args) {
        DoubleNode node1 = new DoubleNode(1, "宋江", "及时雨");
        DoubleNode node2 = new DoubleNode(2, "吴用", "智多星");
        DoubleNode node3 = new DoubleNode(3, "卢俊义", "玉麒麟");
        DoubleNode node4 = new DoubleNode(4, "林冲", "豹子头");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(node1);
        doubleLinkedList.add(node4);
        doubleLinkedList.add(node3);
        doubleLinkedList.add(node2);
        doubleLinkedList.list();

        doubleLinkedList.update(new DoubleNode(-1, "戴宗", "神行太保"));
        System.out.println("修改之后的单链表为:");
        doubleLinkedList.list();

        doubleLinkedList.delete(-1);
        System.out.println("删除之后为:");
        doubleLinkedList.list();

    }

}


class DoubleLinkedList {

    private DoubleNode head = new DoubleNode(-1, "", "");

    /**
     * 删除节点
     */
    public void delete(int no) {
        if (no <= 0) {
            System.out.println("删除节点的no不规范!!!");
            return;
        }
        DoubleNode temp = head.next;
        boolean flag = false;
        while (temp != null) {
            if (temp.no == no) {
                temp.pre.next = temp.next;
                if (temp.next != null) {
                    temp.next.pre = temp.pre;
                }
                flag = true;
            }
            temp = temp.next;
        }

        if (!flag) {
            System.out.println("需要被删除的节点不存在");
        }
    }

    /**
     * 修改节点
     */
    public void update(DoubleNode node) {
        if (node == null || node.no <= 0) {
            System.out.println("被修改的节点不规范!!!");
            return;
        }
        DoubleNode temp = head.next;
        boolean flag = false;
        while (temp != null) {
            if (temp.no == node.no) {
                temp.name = node.name;
                temp.nickname = node.nickname;
                /*node.next = temp.next;
                node.pre = temp.pre;
                temp.pre.next = node;
                if (temp.next != null) {
                    temp.next.pre = node;
                }*/
                flag = true;
            }
            temp = temp.next;
        }
        if (!flag) {
            System.out.println("找不到节点!!!");
        }
    }

    /**
     * 添加节点
     */
    public void add(DoubleNode node) {
        DoubleNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = node;
        node.pre = temp;
    }

    /**
     * 遍历节点
     */
    public void list() {
        DoubleNode temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

}

/**
 * 双向链表的节点
 */
class DoubleNode {

    public int no;
    public String name;
    public String nickname;
    public DoubleNode pre;
    public DoubleNode next;

    public DoubleNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "DoubleNode[" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                ']';
    }
}
